<template>
	<view class="content">
		<!-- <image
			src="https://peiwanblogs.huolieniaowangluo.com/uploads/1/file/20220920/c6c01a45fc39950b72e97e7e586753e2.jpeg"
			mode=""></image> -->
		<button class="login-btn" @click="loginFn">登录</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {

			};
		},
		methods: {
			// 小程序 登录功能
			async loginFn() {
				let that = this;
				let userProfileObject = await that.getUserProfileFn();
				let avatarUrl, nickName, province, city, gender;

				if (userProfileObject.type == 'success') {
					avatarUrl = userProfileObject.result.avatarUrl;
					nickName = userProfileObject.result.nickName;
					province = userProfileObject.result.province;
					city = userProfileObject.result.city;
					gender = userProfileObject.result.gender;
				} else {
					uni.showToast({
						icon: 'none',
						title: '获取个人信息失败'
					});
					return false;
				}

				uni.login({
					async success(e) {
						console.log('先获取code那堆数据', e);

						uni.getUserInfo({
							//获取用户信息
							async success(userInfoRes) {
								let iv = encodeURIComponent(userInfoRes.iv);
								let encryptedData = encodeURIComponent(userInfoRes
									.encryptedData);
								console.log('uni.getUserInfo 成功的数据', userInfoRes, '\n');
								//第一次提示用户授权 成功后的回调 授权后再点直接成功 拿回来用户信息

								console.log('后获取用户信息 包括iv 和 xxxdata字符串', userInfoRes);

								let code = e.code;

								// 登录时 把分享者的字符串带上
								let sharerObj = {};
								let sharerStr = uni.getStorageSync('sharer'); // 带上分享上级的加密字符串
								if (sharerStr) {
									sharerObj.sharer = sharerStr;
								}

								let data = {
									avatar: avatarUrl,
									nickname: nickName,
									area: city,
									sex: gender,
									platform: '小程序',
									code,
									iv,
									encryptedData,

									...sharerObj
								};
								let result = await that.$request({
									loading: true,
									url: '/api/login/third_new',
									data
								});
								console.log('用户登录时带的参数', data, '\n', '返回的结果', result, '\n');

								if (result.statusCode == 200) {
									if (result.data.code == 200) {

										//that.logOutFn(); // 先退出登录一下
										console.log(result.data.data,'result.data.data')
										if (!result.data.data.avatar) {
											uni.setStorageSync('user_reg_xcx', result.data.data
												.open_id);
											uni.navigateTo({
												url: '/pages/login/tags?op=xcx',
											});
										} else {

											let token = result.data.data.login_token;
											that.$store.commit('setToken', token);
											uni.setStorageSync('token', token);
											let user_id = result.data.data.user_id;
											uni.setStorageSync('user_id', user_id);
											that.$emit('loginok', true);
											that.bind()
											uni.hideLoading();
											uni.showLoading({});
											that.hasTokenGetUserInfo(() => {
												uni.hideLoading();
												uni.showToast({
													icon: 'success',
													title: '登录成功'
												});
												that.$emit('loginok', true);
												that.$emit('loginSuccessCallback');
												setTimeout(() =>{
													uni.switchTab({
														url:"/pages/index"
													})
												},800)
												// that.closeGlobalLoginFn(false);
											});
										}
									} else {
										uni.showToast({
											icon: 'none',
											title: result.data.msg
										});
									}
								} else {
									uni.showToast({
										icon: 'none',
										title: that.$errorMsg
									});
								}
							},
							fail(e) {
								console.log('登录失败 小程序 getUserInfo 接口fail ', e);
							}
						});
					}
				});
			},
			bind() {
				let msg = {
					'type': 'bind',
					'fromid': uni.getStorageSync('user_id')
				};
				uni.sendSocketMessage({
					data: JSON.stringify(msg),
					success: (res) => {
						console.log('login绑定会员id成功' + uni.getStorageSync('user_id'), res)
					},
					fail: (res) => {
						//console.log('绑定会员id失败'+uni.getStorageSync('user_id'), res)
					}
				});

			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		height: 90vh;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		image {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
		}

		.login-btn {
			display: flex;
			justify-content: center;
			align-items: center;
			margin: 35rpx auto 0;
			width: 536rpx;
			height: 88rpx;
			color: #ffffff;
			font-size: 30rpx;
			background: linear-gradient(180deg, #DB89F5 0%, #834BF4 100%);
		}
	}
</style>